
import { _decorator, Component, Node } from 'cc';
import { DEBUG } from 'cc/env';
import { BundlesConst } from '../../const/Const';
import { resourceUtil } from './resourceUtil';

const { ccclass, property } = _decorator;
@ccclass('BundleMgr')
export class BundleMgr extends Component {

    private static loadCache: { [bundleKey: string]: { loaded: boolean, hasLoadAd: boolean, callback: Array<(hasLoad: boolean) => void> } } = {};
    /**
     * 
     * @param nameOrUrl bundle的key或者url
     * @param onProgress 
     * @param onComplete 
     * @param entityDir 
     * @param showLoading 
     */
    public static loadBundleOnce(nameOrUrl: string, onProgress?: (bundleKey: string, precent: number) => void, onComplete?: (hasLoad: boolean) => void, entityDir: string = "", showLoading: boolean = true) {

        if (this.loadCache.hasOwnProperty(nameOrUrl)) {
            if (this.loadCache[nameOrUrl].loaded) {
                if (onComplete)
                    onComplete(this.loadCache[nameOrUrl].hasLoadAd)
            }
            else {
                this.loadCache[nameOrUrl].callback.push(onComplete)
            }
        }
        else {
            this.loadCache[nameOrUrl] = { loaded: false, hasLoadAd: false, callback: [onComplete] };
            resourceUtil.loadSubEntity(nameOrUrl, (percent) => {
                if (onProgress)
                    onProgress(nameOrUrl, percent);
            }, () => {
                // resourceUtil.loadBundleSound(nameOrUrl, () => { }, () => {
                //     this.loadCache[nameOrUrl].loaded = true;
                //     this.loadCache[nameOrUrl].callback.forEach((call: Function) => {
                //         call(this.loadCache[nameOrUrl].hasLoadAd)
                //     })
                //     this.loadCache[nameOrUrl].callback = [];
                // });
                this.loadCache[nameOrUrl].loaded = true;
                this.loadCache[nameOrUrl].callback.forEach((call: Function) => {
                    call(this.loadCache[nameOrUrl].hasLoadAd)
                })
                this.loadCache[nameOrUrl].callback = [];
            }, entityDir, showLoading)
        }
    }
    /**
     * 加载多个bundle
     * @param bundleKeys bundle的key
     * @param onProgress 加载进度
     * @param completed 加载完成
     * @param showLoading 显示加载进度
     */
    public static loadMultiBundle(bundleKeys: string[], onProgress?: (percents: Array<{ bundleKey: string, percent: number }>) => void, completed?: () => void, showLoading: boolean = true) {
        let bundleKeyCount = bundleKeys.length;
        let completedCount = 0;
        let percents: Array<{ bundleKey: string, percent: number }> = [];
        bundleKeys.forEach(bundleKey => {
            percents.push({ bundleKey: bundleKey, percent: 0 });
        })
        let onProgressCall = (bundleKey: string, percent: number) => {
            let currentIndex = percents.findIndex(item => item.bundleKey == bundleKey);
            if (currentIndex != -1)
                percents[currentIndex].percent = percent;
            else
                percents.push({ bundleKey: bundleKey, percent: percent });
            if (onProgress)
                onProgress(percents);
        }
        let onCompleteCall = (hasLoad: boolean) => {
            completedCount++;
            if (completedCount == bundleKeyCount) {
                if (completed)
                    completed();
            }
            else {
                this.loadBundleOnce(bundleKeys[completedCount], onProgressCall, onCompleteCall, undefined, showLoading);
            }
        }
        this.loadBundleOnce(bundleKeys[completedCount], onProgressCall, onCompleteCall, undefined, showLoading);
    }

}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
